package com.huawei.zyb.taskManager.domain.requirement.valueobject;

import java.util.Objects;

/**
 * 需求唯一标识值对象
 */
public final class RequirementId {
    private final Long value;

    private RequirementId(Long value) {
        this.value = Objects.requireNonNull(value, "需求ID不能为空");
        if (value <= 0) {
            throw new IllegalArgumentException("需求ID必须为正数");
        }
    }

    public static RequirementId of(Long value) {
        return new RequirementId(value);
    }
    
    public static RequirementId of(String value) {
        if (value == null || value.trim().isEmpty()) {
            throw new IllegalArgumentException("需求ID字符串不能为空");
        }
        // 如果是REQ-001这样的格式，提取数字部分
        if (value.startsWith("REQ-")) {
            String numberPart = value.substring(4);
            try {
                return new RequirementId(Long.parseLong(numberPart));
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("无效的需求ID格式: " + value);
            }
        }
        // 直接解析为Long
        try {
            return new RequirementId(Long.parseLong(value));
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("无效的需求ID格式: " + value);
        }
    }

    public Long getValue() {
        return value;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        RequirementId that = (RequirementId) obj;
        return Objects.equals(value, that.value);
    }

    @Override
    public int hashCode() {
        return Objects.hash(value);
    }

    @Override
    public String toString() {
        return "RequirementId{" + value + "}";
    }
}